/*
 * Copyright 2005-2013 finalist.cn. All rights reserved.
 * Support: http://www.finalist.cn
 * License: http://www.finalist.cn/license
 */
package com.finalist.dao.impl;

import java.util.List;

import javax.persistence.FlushModeType;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;

import com.finalist.Page;
import com.finalist.Pageable;
import com.finalist.dao.AdminDao;
import com.finalist.entity.Admin;

/**
 * Dao - 管理员
 * 
 * @author Finalist IT Group
 * @version 3.0
 */
@Repository("adminDaoImpl")
public class AdminDaoImpl extends BaseDaoImpl<Admin, Long> implements AdminDao {
    private static final Log log = LogFactory.getLog(AdminDaoImpl.class);
	public boolean usernameExists(String username) {
		if (username == null) {
			return false;
		}
		String jpql = "select count(*) from Admin admin where lower(admin.username) = lower(:username)";
		Long count = entityManager.createQuery(jpql, Long.class).setFlushMode(FlushModeType.COMMIT).setParameter("username", username).getSingleResult();
		return count > 0;
	}

	public Admin findByUsername(String username) {
		if (username == null) {
			return null;
		}
		try {
			String jpql = "select admin from Admin admin where lower(admin.username) = lower(:username)";
			List<Admin> lists = entityManager.createQuery(jpql, Admin.class).setFlushMode(FlushModeType.COMMIT).setParameter("username", username).getResultList();
			if(lists!=null&&lists.size()>0){
				return lists.get(0);
			}
			return null;
		} catch (Exception e) {
        	e.printStackTrace();
        	log.error(e.toString());
			return null;
		}
	}
	
	public Page<Admin> findPage(Integer flag,Pageable pageable){
		CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
		CriteriaQuery<Admin> criteriaQuery = criteriaBuilder
				.createQuery(Admin.class);
		Root<Admin> root = criteriaQuery.from(Admin.class);
		criteriaQuery.select(root);
		Predicate restrictions = criteriaBuilder.conjunction();
		if(flag!=null){
			Boolean isCheck = null;
			if(flag == 1){
				isCheck = true;
			}
			if(flag == 2 || flag == 3){
				isCheck = false;
			}
			if(flag == 4){}
			if (isCheck != null) {
				if(isCheck){
					restrictions = criteriaBuilder.and(restrictions, criteriaBuilder
							.equal(root.get("isCheck"), isCheck));
				}else{
					if(flag == 3){
						restrictions = criteriaBuilder.and(restrictions, criteriaBuilder
								.isNull(root.get("reason")), criteriaBuilder.equal(root.get("isCheck"), false));
					}
					if(flag == 2){
						restrictions = criteriaBuilder.and(restrictions, criteriaBuilder
								.isNotNull(root.get("reason")), criteriaBuilder.equal(root.get("isCheck"), false));
					}
				}
			}
		}
		
		criteriaQuery.where(restrictions);
		return super.findPage(criteriaQuery, pageable);
	}

	@Override
	public void updateActionEamil(Long id) {
		try {
			String deleteSql = "update Admin a set a.hasEmailAction = true , a.step = 2 where a.id = "+ id;
			entityManager.createQuery(deleteSql).setFlushMode(FlushModeType.COMMIT).executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public Admin findByEmail(String email) {
		if (email == null) {
			return null;
		}
		try {
			String jpql = "select admin from Admin admin where lower(admin.email) = lower(:email)";
			List<Admin> lists = entityManager.createQuery(jpql, Admin.class).setFlushMode(FlushModeType.COMMIT).setParameter("email", email).getResultList();
			if(lists!=null&&lists.size()>0){
				return lists.get(0);
			}
			return null;
		} catch (Exception e) {
        	e.printStackTrace();
        	log.error(e.toString());
			return null;
		}
	}


}